//
//  main.m
//  07-MRCHomeWork
//
//  Created by 曹曹 on 16/7/12.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Town.h"
#import "TempCity.h"

int main(int argc, const char * argv[]) {
    //    1.使用MRC, 重写用类处理的两个省市区。加入对应内存管理。
    //    2.复习Objective-C语言的基本特性
#pragma mark - 1
    NSString *sourceData = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/Objective-C/HomeWork/05-CollectionTraversalHomeWork/05-CollectionTraversalHomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *data = [sourceData componentsSeparatedByString:@"\n"];
    NSMutableArray *provinceObjectArray = [NSMutableArray array];
    for (NSString *area in data) {
        if (![area hasPrefix:@" "]) {
            Province *prov = [Province townWithName:area];
            [provinceObjectArray addObject:prov];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *prov = [provinceObjectArray lastObject];
            City *city = [City townWithName:area];
            [prov.array addObject:city];
        } else {
            City *city = [[[provinceObjectArray lastObject] array] lastObject];
            Town *town = [Town townWithName:area];
            [city.array addObject:town];
        }
    }
    NSLog(@"%@", provinceObjectArray);
#pragma mark - 2
    NSString *sourceData2 = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/Objective-C/HomeWork/05-CollectionTraversalHomeWork/05-CollectionTraversalHomeWork/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    // 根据","分割字符串, 得到所有城市的数组
    NSArray *allCityArray = [sourceData2 componentsSeparatedByString:@","];
#pragma mark - 转换成类
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
        [tempCity release];
    }
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    [sortTempCity release];
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *prov = [Province townWithTempCity:tempCity];
            [finalClassArray addObject:prov];
        } else if ([tempCity.cityNumber hasSuffix:@"00"]) {
            Province *prov = [finalClassArray lastObject];
            City *city = [City townWithTempCity:tempCity];
            [prov.array addObject:city];
        } else {
            Province *prov = [finalClassArray lastObject];
            City *city = [prov.array lastObject];
            Town *town = [Town townWithTempCity:tempCity];
            [city.array addObject:town];
        }
    }
        NSLog(@"%@", finalClassArray);
    return 0;
}
